package com.youyun56.gps.android;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.youyun56.gps.android.dao.ImDao;
import com.youyun56.gps.android.pojo.Im;

public class ImItemAdapter extends BaseAdapter implements OnClickListener {
    private ImsActivity activity;
    private LinkedList<Im> ims = new LinkedList<Im>();
    private ListView list;

    private final static int maxSize = 100;

    public ImItemAdapter() {
        // TODO Auto-generated constructor stub
    }

    public ImItemAdapter(ImsActivity context) {

        this.activity = context;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return ims.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return ims.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {

        // SimpleAdapter
        if (view == null) {
            LayoutInflater layoutInflater = LayoutInflater.from(activity);
            view = layoutInflater.inflate(R.layout.imitem, null);
        }
        final Im im = ims.get(position);
        TextView context = (TextView) view.findViewById(R.id.context);
        context.setText(im.getContext());
        TextView attach = (TextView) view.findViewById(R.id.attach);
        attach.setText(im.getAttach());
        TextView delIm = (TextView) view.findViewById(R.id.delIm);
        delIm.setOnClickListener(this);
        TextView addTime = (TextView) view.findViewById(R.id.addTime);
        addTime.setText(im.getAddDay());
        return view;
    }

    public void addItem(Im im) {
        if (im != null) {
            ims.addFirst(im);
            notifyDataSetChanged();
            activity.getEmptyView().setVisibility(View.GONE);
            if (ims.size() > maxSize) {
                removeLast();
            }
        }
    }

    private void removeLast() {
        remove(ims.size() - 1);
    }

    public void addAll(List<Im> imss) {
        if (imss != null && !imss.isEmpty()) {
            for (Iterator<Im> iterator = imss.iterator(); iterator.hasNext();) {
                Im im = (Im) iterator.next();
                addItem(im);
            }
            notifyDataSetChanged();
        }
    }

    public void remove(int position) {
        ims.remove(position);
        notifyDataSetChanged();
        if (ims.size() == 0) {
            activity.getEmptyView().setVisibility(View.VISIBLE);
        }
    }

    public void removeAll() {
        ims.clear();
        notifyDataSetChanged();
    }

    public void setListView(ListView list) {
        this.list = list;
    }

    public ListView getList() {
        return list;
    }

    @Override
    public void onClick(final View v) {
        new AlertDialog.Builder(activity).setTitle("删除").setMessage("删除此条消息？")// 设置内容
                .setPositiveButton("确定",// 设置确定按钮
                        new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                LinearLayout layout = (LinearLayout) v.getParent();
                                int position = ImItemAdapter.this.getList().getPositionForView(layout);
                                Im im = ims.get(position);
                                ImDao.getInstance().delImById(String.valueOf(im.getId()));
                                remove(position);
                            }

                        }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        dialog.dismiss();
                    }
                }).create().show();

    }
}
